package com.hspedu.newData.dmsxl.树.二叉树路径.路径;

import com.hspedu.newData.dmsxl.ds.TreeNode;
import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-23 
 * @Version: 1.0
 */

public class LC112路径总和 {

    public int sum = 0;

    public boolean hasPathSum(TreeNode root, int targetSum) {

        if (root == null){
            return false;
        }
        sum += root.val;
        if (root.left == null && root.right == null){
            return sum == targetSum;
        }
        if (root.left != null){
            Boolean b = hasPathSum(root.left, targetSum);
            sum -= root.left.val;
            if (b){
                return true;
            }
        }
        if (root.right != null){
            Boolean b = hasPathSum(root.right, targetSum);
            sum -= root.right.val;
            if (b){
                return true;
            }
        }
        return false;
    }

    @Test
    public void test() {
        // 测试代码

        TreeNode t7 = new TreeNode(7);
        TreeNode t6 = new TreeNode(6);
        TreeNode t5 = new TreeNode(5);
        TreeNode t4 = new TreeNode(4);
        TreeNode t3 = new TreeNode(3,t6,t7);
        TreeNode t2 = new TreeNode(2,t4,t5);
        TreeNode t1 = new TreeNode(1,t2,t3);

        boolean b = hasPathSum(t1, 10);
        System.out.println("==========");

    }
}
